using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using DTFight;

public class UIResultList : MonoBehaviour
{
	public UIGameResult PrefabResult;
	public Transform GridResult;

	void Awake() {
		ClearResult();
	}

	// Use this for initialization
	void Start ()
	{

	}

	// Update is called once per frame
	void Update ()
	{
		UpdateUI();
	}

	void UpdateUI()
	{
		if (GridResult == null || PrefabResult == null)
			return;

		bool repos = false;
		List<GameData.ResultInfo> rtlist = UIGameMgr.CurRoom.ResultList;
		for (int i=0; i<rtlist.Count; ++i)
		{
			if (i < GridResult.childCount)
			{
				GameObject go = GridResult.GetChild(i).gameObject;

				UIGameResult uiGR = go.GetComponent<UIGameResult>();
				uiGR.InstData = rtlist[i];
			}
			else
			{
				GameObject go = Instantiate(PrefabResult.gameObject) as GameObject;
				go.transform.SetParent(GridResult);
				go.transform.localPosition = Vector3.zero;
				go.transform.localScale = Vector3.one;

				UIGameResult uiGR = go.GetComponent<UIGameResult>();
				uiGR.InstData = rtlist[i];
				
				if (uiGR.TxIndex != null) uiGR.TxIndex.text = (i+1).ToString();

				repos = true;
			}


		}

		if (repos)
		{
			VerticalLayoutGroup vlg = GridResult.GetComponent<VerticalLayoutGroup>();
			if (vlg != null)
			{
				RectTransform rt = GridResult.GetComponent<RectTransform>();
				float h = vlg.spacing * GridResult.childCount;
				rt.localPosition = new Vector3(0, -h/2, 0);
				rt.sizeDelta = new Vector2(0,  h);
				
			}
		}
	}

	void ClearResult()
	{
		if (GridResult == null)
			return;

		GridResult.DetachChildren();
	}
}

